﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace Chromium_Updater
{
    public partial class Settings : Form
    {
        private bool valid_latest = true;
        private bool valid_specific = true;

        public Settings()
        {
            InitializeComponent();
        }

        #region Open & Close

        private void Settings_Load(object sender, EventArgs e)
        {
            textBox1.Text        = SettingsManager.latest_revision_url;
            numericUpDown1.Value = Convert.ToDecimal(SettingsManager.update_frequency);
            textBox3.Text        = SettingsManager.specific_revision_url;
            checkBox2.Checked    = SettingsManager.start_hidden;
            checkBox3.Checked    = SettingsManager.auto_download;
            checkBox1.Checked    = Util.IsAutoStartEnabled("Chromium Updater", Application.StartupPath + "\\Chromium Updater.exe");
        }

        private void Settings_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (valid_latest) { SettingsManager.latest_revision_url = textBox1.Text.Trim(); }
            else
            {
                if (SettingsManager.validLatestURL(textBox1.Text.Trim()))
                {
                    SettingsManager.latest_revision_url = textBox1.Text.Trim();
                }
                else
                {
                    MessageBox.Show("The latest revision URL is invalid.");
                    e.Cancel = true;
                }
            }
            if (valid_specific) { SettingsManager.specific_revision_url = textBox3.Text.Trim(); }
            else
            {
                if (SettingsManager.validSpecificURL(textBox3.Text.Trim()))
                {
                    SettingsManager.specific_revision_url = textBox3.Text.Trim();
                }
                else
                {
                    MessageBox.Show("The specific revision URL is invalid.");
                    e.Cancel = true;
                }
            }
            try { SettingsManager.update_frequency = Convert.ToInt32(numericUpDown1.Value); }
            catch
            {
                MessageBox.Show("Only numeric characters can applied for update frequency.");
                e.Cancel = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Process.Start("http://tlokzz.info");
        }

        #endregion

        #region Checkboxes

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                Util.SetAutoStart("Chromium Updater", Application.StartupPath + "\\Chromium Updater.exe");
            }
            else
            {
                Util.UnSetAutoStart("Chromium Updater");
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            SettingsManager.start_hidden = checkBox2.Checked;
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            SettingsManager.auto_download = checkBox3.Checked;
        }

        #endregion

        #region Checks & Reset

        private void button2_Click(object sender, EventArgs e)
        {
            if (SettingsManager.validLatestURL(textBox1.Text.Trim()))
            {
                valid_latest = true;
                MessageBox.Show("The Latest Revision URL is valid.");
            }
            else
            {
                MessageBox.Show("Please enter a valid Latest Revision URL.");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (SettingsManager.validSpecificURL(textBox3.Text.Trim()))
            {
                valid_specific = true;
                MessageBox.Show("The Specific Revision URL is valid.");
            }
            else
            {
                MessageBox.Show("Please enter a valid Specific Revision URL.");
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            valid_latest = false;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            valid_specific = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("This will reset all your settings. Are you sure you want to do this?", "Chromium Updater", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                SettingsManager.latest_revision_url   = "http://commondatastorage.googleapis.com/chromium-browser-continuous/Win/LAST_CHANGE";
                SettingsManager.specific_revision_url = "http://commondatastorage.googleapis.com/chromium-browser-continuous/Win/";
                SettingsManager.download_dir  = "%CURRENT_DIR%\\Downloads";
                SettingsManager.start_hidden  = false;
                SettingsManager.auto_download = false;
                SettingsManager.update_frequency = 60;
                //
                Settings_Load(this, null);
            }
        }

        #endregion
    }
}
